package com.eastjet.equipment.controller;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.eastjet.equipment.service.DeviceExecutionService;
import com.eastjet.maintenance.domain.DeviceClass;
import com.eastjet.maintenance.domain.DeviceExecution;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.Map;

@CrossOrigin
@RestController
@RequestMapping("/execution")
public class ExecutionController {

    @Autowired
    private DeviceExecutionService deviceExecutionService;

    @RequestMapping("/list")
    public Page<DeviceExecution> executionlist(@RequestParam(defaultValue = "1") int page,
                                               @RequestParam(defaultValue = "2") int size,
                                               @RequestParam(defaultValue ="") String condition)
    {
        System.out.println("condition:" + condition);
        return deviceExecutionService.getExecutionList(page, size,condition);
    }

    @RequestMapping("/add")
    public String add(@RequestBody Map<String, Object> map)
    {
        System.out.println("map:" + map);
        if (map == null || map.isEmpty()) {
            return "参数不能为空";
        }
        DeviceExecution deviceExecution = new DeviceExecution();
        deviceExecution.setExecutionName(map.get("executionName").toString());
        if (map.get("executionType").toString().equals("小时")){
            deviceExecution.setExecutionType("0");
        } else if (map.get("executionType").toString().equals("日")) {
            deviceExecution.setExecutionType("1");
        }else if (map.get("executionType").toString().equals("周")){
            deviceExecution.setExecutionType("2");
        }else if (map.get("executionType").toString().equals("月")){
            deviceExecution.setExecutionType("3");
        }else if (map.get("executionType").toString().equals("季")){
            deviceExecution.setExecutionType("4");
        }else {
            deviceExecution.setExecutionType("5");
        }
        if (map.get("executionInterval") != null||map.get("executionInterval")=="0"){
            deviceExecution.setExecutionInterval("-");
        }else {
            deviceExecution.setExecutionInterval(map.get("executionInterval").toString()+map.get("executionType").toString());
        }
        deviceExecution.setExecutionTime(map.get("executionTime").toString());
        deviceExecution.setExecutionDescribe(map.get("executionDescribe").toString());
        deviceExecution.setExecutionFrequency("每"+map.get("executionInterval").toString()+map.get("executionType").toString());
        return deviceExecutionService.addDeviceExecution(deviceExecution);
    }

    @RequestMapping("/update")
    public String update(@RequestBody Map<String, Object> map)
    {
        System.out.println("map:" + map);
        if (map == null || map.isEmpty()) {
            return "参数不能为空";
        }
        DeviceExecution deviceExecution = new DeviceExecution();
        deviceExecution.setExecutionId(Integer.valueOf(map.get("executionId").toString()));
        deviceExecution.setExecutionName(map.get("executionName").toString());
        if (map.get("executionType").toString().equals("小时")){
            deviceExecution.setExecutionType("0");
        } else if (map.get("executionType").toString().equals("日")) {
            deviceExecution.setExecutionType("1");
        }else if (map.get("executionType").toString().equals("周")){
            deviceExecution.setExecutionType("2");
        }else if (map.get("executionType").toString().equals("月")){
            deviceExecution.setExecutionType("3");
        }else if (map.get("executionType").toString().equals("季")){
            deviceExecution.setExecutionType("4");
        }else {
            deviceExecution.setExecutionType("5");
        }
        if (map.get("executionInterval") != null||map.get("executionInterval")=="0"){
            deviceExecution.setExecutionInterval("-");
        }else {
            deviceExecution.setExecutionInterval(map.get("executionInterval").toString()+map.get("executionType").toString());
        }
        deviceExecution.setExecutionTime(map.get("executionTime").toString());
        deviceExecution.setExecutionDescribe(map.get("executionDescribe").toString());
        deviceExecution.setExecutionFrequency("每"+map.get("executionInterval").toString()+map.get("executionType").toString());
        return deviceExecutionService.updateDeviceExecution(deviceExecution);
    }

    @RequestMapping("/delete")
    public String delete(@RequestBody Map<String, Object> map)
    {
        System.out.println("map:" + map);
        if (map == null || map.isEmpty()) {
            return "参数不能为空";
        }
        DeviceExecution deviceExecution = new DeviceExecution();
        deviceExecution.setExecutionId(Integer.valueOf(map.get("executionId").toString()));
        return deviceExecutionService.deleteDeviceExecution(deviceExecution);
    }

    @RequestMapping("/updateState")
    public String updateState(@RequestBody Map<String, Object> map)
    {
        System.out.println("map:" + map);
        if (map == null || map.isEmpty()) {
            return "参数不能为空";
        }
        DeviceExecution deviceExecution = new DeviceExecution();
        deviceExecution.setExecutionId(Integer.valueOf(map.get("executionId").toString()));
        deviceExecution.setExecutionState(Integer.valueOf(map.get("executionState").toString()));
        return deviceExecutionService.updateDeviceExecutionState(deviceExecution);
    }
}
